﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder
{
    /// <summary>
    /// 港口
    /// </summary>
    class B1Form_Port : SAPBoAddon.B1Form
    {
        public B1Form_Port()
        {
            frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Port_frm_ChooseFormListEvent);
            frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_Port_frm_FormDataEvent);
        }

        #region 继承基类
        protected override bool BeforeSaveCheckFormData()
        {
            //港口编码 
            if (this.IsPortCodeNull())
            {
                ShowMessageInformation("29");
                return false;
            }

            ////城市编号
            //if (this.IsCityCodeNull())
            //{
            //    ShowMessageInformation("5");
            //    return false;
            //}

            return true;

        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_Port;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_FF_Port.ToString() + "Form.srf";

            #region 控件是否可编辑
            //FormSetting.FormModeSetting.StatusItemUID = "7";
            //FormSetting.FormModeSetting.DisableStatus = new string[] { "R", "F", "C" };
            //FormSetting.FormModeSetting.itmBrowseModeDisable = Utils.GetFormItems(FormSetting.FormXmlDocument);
            FormSetting.FormModeSetting.itmFindModeEnable = new string[] { "26" };

            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
        }
        #endregion
        #region 事件处理
        void B1Form_Port_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = default(SAPbouiCOM.DBDataSource);
            if (pVal.BeforeAction)
            {

            }
            else
            {
                switch (pVal.ChooseFromListUID)
                {
                    //国家
                    case "CFL_OCRY":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_PORT");
                        dbds.SetValue("U_CountryCode", dbds.Offset, pVal.SelectedObjects.GetValue("Code", 0).ToString());
                        dbds.SetValue("U_CountryNameC", dbds.Offset, pVal.SelectedObjects.GetValue("Name", 0).ToString());
                        dbds.SetValue("U_CountryNameE", dbds.Offset, pVal.SelectedObjects.GetValue("U_ForeignName", 0).ToString());
                        break;
                }
            }
        }

        void B1Form_Port_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction )
            {
                if (pVal.EventType==BoEventTypes.et_FORM_DATA_ADD)
                {
                    this.SetCode();
                }
            }
        }
        #endregion
        #region 私有函数
        /// <summary>
        /// 显示中文信息还是英文提示信息
        /// </summary>
        /// <param name="UItemId"></param>
        private void ShowMessageInformation(string UItemId)
        {
            SAPbouiCOM.Form oFrom;
            oFrom = this.CurrentForm;
            string information = SAPBoAddon.B1Assistant.Item.GetValue(oFrom, UItemId).ToString();
            if (SBOCompany.language.ToString().Trim().Contains("Chinese"))
            {
                SBOApp.StatusBar.SetText(information + "不能为空！", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
            }
            else
            {
                SBOApp.StatusBar.SetText(information + " IS NOT NULL！", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
            }
        }

        /// <summary>
        /// 判断港口编码是否为空
        /// </summary>
        /// <returns></returns>
        private bool IsPortCodeNull()
        {
            SAPbouiCOM.Form oForm;
            oForm = this.CurrentForm;
            string portCode = SAPBoAddon.B1Assistant.Item.GetValue(oForm, "30").ToString();
            if (portCode.Trim().Length == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断城市编号是否为空
        /// </summary>
        /// <returns></returns>
        private bool IsCityCodeNull()
        {
            SAPbouiCOM.Form oForm;
            oForm = this.CurrentForm;
            string cityCode = SAPBoAddon.B1Assistant.Item.GetValue(oForm, "6").ToString();
            if (cityCode.Trim().Length == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 设置Code主键值
        /// </summary>
        private void SetCode()
        {
            Recordset oRst = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            oRst.DoQuery("select ISNULL(MAX(DocEntry),1) as NextCode FROM [@AVA_FF_PORT]");
            int max = 1;
            if (oRst.RecordCount > 0)
            {
                max = Convert.ToInt32(oRst.Fields.Item("NextCode").Value) + 1;
            }
            this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_PORT").SetValue("Code", 0, max.ToString());
        }

        #endregion
    }
}
